package com.fruit.controller.app.login;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fruit.constants.UserConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.user.SupplierReal;
import com.fruit.pojo.user.UserInfo;
import com.fruit.pojo.user.UserLogin;
import com.fruit.service.user.UserManager;
import com.fruit.util.MD5Util;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.ValidateUtils;



@Controller
@RequestMapping(value = "/app/")
public class LoginController extends BaseController {

	@Autowired
	private UserManager userService;
	
	
	// app用户登录
	@RequestMapping(value = "login", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> login(UserLogin ul,HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "用户登录");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			String userName = ul.getUserName();
			if(!ValidateUtils.phone(userName)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "phone_empty", "手机号码格式错误!"));
				return resMap;
			}
			String pwd = ul.getPwd();
			if(!ValidateUtils.password(pwd)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "pwd_empty", "密码需6-20位数字和字母组成!"));
				return resMap;
			}
			
			//查看用戶是否存在
			UserLogin login = userService.getUserLoginByUserName(userName);
			if(login == null){
				resMap.put("result", this.initResult(ResultInfo.ERROR, "exists", "用户不存在!"));
				return resMap;
			}
		
			if(!StringUtils.equals(login.getPwd(), MD5Util.MD5Encode(pwd, "utf-8"))){
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "密码错误!"));
				return resMap;
			}
			
			UserInfo userInfo = userService.getUserInfoByLoginId(login.getId());
			
			if (!"01".equals(userInfo.getState())) {
				String msg = "该用户已经被禁用，请联系管理员";
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", msg));
				return resMap;
			}
			
			resMap.put("userInfo", userInfo);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "登录成功!"));
		
			
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	// app用户注册
	@RequestMapping(value = "registerValidate", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> registerValidate(UserLogin ul,HttpServletResponse response, HttpServletRequest request){
		this.logBefore(logger, "用户注册验证");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = userService.getRegisterValidate(ul);
			
		}catch(Exception e){
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
		
	}
	
	
	
	
	
	// app用户注册
	@RequestMapping(value = "register", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> register(UserLogin ul,HttpServletResponse response, HttpServletRequest request){
		this.logBefore(logger, "用户注册");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = userService.getRegisterValidate(ul);
			ResultInfo ri = (ResultInfo)resMap.get("result");
			if(ResultInfo.SUCCESS.equals(ri.getState())){
				SupplierReal sr = (SupplierReal)resMap.get("sr");
				resMap.clear();
				if(!StringUtils.equals(ul.getBusLicenseNumber(), sr.getBusLicenseNumber())){
					resMap.put("result", this.initResult(ResultInfo.ERROR, "busLicense_error", "社会统一信用代码错误!"));
					return resMap;
				}
				//调用注册接口
				sr.setIsUseInviteCode(UserConstant.USE_INVITECODE);
				resMap = userService.saveUserRegister(ul, sr);
			}
			
		}catch(Exception e){
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
		
	}
	
	//修改密码
	@RequestMapping(value = "setPwd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> setPwd(UserLogin ul,HttpServletResponse response, HttpServletRequest request){
		this.logBefore(logger, "修改登录密码");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = userService.updatePwd(ul);

		}catch(Exception e){
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
		
	}
	
	@RequestMapping(value = "outLogin", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> outLogin(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
		request.getSession().removeAttribute("sessionUserId");
		resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", "退出登录成功!"));
		}catch(Exception e){
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		return resMap;
	}
	

	
}
